package com.senmo.activity.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.senmo.activity.main.MainActivity;
import com.senmo.base.ActionBar;
import com.senmo.base.BaseActivity;
import com.senmo.manager.AccountManager;
import com.senmo.manager.UserCallback;
import com.senmo.model.User;
import com.senmo.request.DataHull;
import com.senmo.request.ErrorHandler.ErrorHandler;
import com.senmo.senmo.R;

/**
 * author: lx
 * date: 16-1-4
 */
public class LoginActivity extends BaseActivity implements
        View.OnClickListener,
        ActionBar.Default.Callback {

    private EditText mEditUserName;
    private EditText mEditPassword;
    private View mUserNameClear;
    private View mPasswordClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEditUserName = obtainView(R.id.login_edit_username);
        mEditPassword = obtainView(R.id.login_edit_password);
        mUserNameClear = obtainView(R.id.login_edit_username_clear);
        mPasswordClear = obtainView(R.id.login_edit_password_clear);
        mUserNameClear.setOnClickListener(this);
        mUserNameClear.setOnFocusChangeListener(mEditorFocusChangeListener);
        mPasswordClear.setOnClickListener(this);
        mPasswordClear.setOnFocusChangeListener(mEditorFocusChangeListener);
        findViewById(R.id.login_btn_login).setOnClickListener(this);
        findViewById(R.id.login_btn_register).setOnClickListener(this);
        findViewById(R.id.login_btn_forget_password).setOnClickListener(this);
    }

    @Override
    public void onActionBarCreated(View actionBar, ImageView left, TextView title, ImageView right) {
        left.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_edit_username_clear: {
                mEditUserName.getText().clear();
                break;
            }
            case R.id.login_edit_password_clear: {
                mEditPassword.getText().clear();
                break;
            }
            case R.id.login_btn_login: {
                login();
                break;
            }
            case R.id.login_btn_forget_password: {
                // TODO
                break;
            }
            case R.id.login_btn_register: {
                // TODO
                break;
            }
        }
    }

    View.OnFocusChangeListener mEditorFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean c) {
            int id = view.getId();
            switch (id){
                case R.id.login_edit_username: {
                    int length = mEditUserName.getText().toString().length();
                    mUserNameClear.setVisibility((c && length > 0) ? View.VISIBLE : View.GONE);
                    break;
                }
                case R.id.login_edit_password: {
                    int length = mEditPassword.getText().toString().length();
                    mPasswordClear.setVisibility((c && length > 0) ? View.VISIBLE : View.GONE);
                    break;
                }
            }
        }
    };

    private void login() {
        String username = mEditUserName.getText().toString();
        String password = mEditPassword.getText().toString();
        if (TextUtils.isEmpty(username)) {
            toastShort(R.string.login_toast_username_empty);
            return;
        }
        if (TextUtils.isEmpty(password)) {
            toastShort(R.string.login_toast_password_empty);
            return;
        }

        showLoadingDialog(R.string.login_progress_message);
        AccountManager.getInstance().login(username, password, new UserCallback() {
            @Override
            public void onSuccess(User user) {
                dismissLoadingDialog();
                startActivity(MainActivity.class);
                finish();
            }

            @Override
            public void onFail(int error, DataHull datahull) {
                if (datahull != null) {
                    ErrorHandler.typeToast().handleError(datahull);
                }
                dismissLoadingDialog();
            }
        });
    }
}
